package com.smart.carey.util;


import com.alibaba.fastjson.JSONObject;
import com.smart.carey.model.StandardDatagram;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author : Carey
 * @version 1.0
 * @descrition :
 * @date :
 */

public abstract class BaseController {

    private static Logger logger = LoggerFactory.getLogger(BeanUtils.class);

    private final static String PAGENUM = "page_num";
    private final static String PAGESIZE = "page_size";

    protected class CheckParam {
        private boolean myResult;
        private Map<String, Object> objectMap;
        private StandardDatagram sd;

        public CheckParam(Map<String, Object> objectMap) {
            this.objectMap = objectMap;
        }

        public boolean is() {
            return myResult;
        }

        public StandardDatagram getSd() {
            return sd;
        }

        public CheckParam invoke(String [] param) {

            for (int i =0;i<param.length;i++){
                if(null == objectMap.get(param[i])){
                    sd = new StandardDatagram.Builder()
                            .createFailed().setErrCode(StandardDatagram.PARAM_VALIDATION_ERROR02)
                            .setException(param[i] + " 参数缺失！！！").build();
                    myResult = true;
                    return this;
                }
                if(null != objectMap.get(param[i]) && objectMap.get(param[i]).toString().trim().length() == 0){
                    sd = new StandardDatagram.Builder()
                            .createFailed().setErrCode(StandardDatagram.PARAM_VALIDATION_ERROR02)
                            .setException(param[i] + " 参数为空字符串 ！！！").build();
                    myResult = true;
                    return this;
                }
            }
            myResult = false;
            return this;
        }
    }


    /**
     * 判断list 是否包含重复数据
     * @param list
     * @return 存在返回false 不存在返回 true
     */
    public static boolean hasSame(List<? extends Object> list)
    {
        if(null == list)
            return false;
        return list.size() == new HashSet<Object>(list).size();
    }

    @SuppressWarnings("rawtypes")
    public  Map toMap(Object bean) {
        Class<? extends Object> clazz = bean.getClass();
        Map<Object, Object> returnMap = new HashMap<Object, Object>();
        BeanInfo beanInfo = null;
        try {
            beanInfo = Introspector.getBeanInfo(clazz);
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (!propertyName.equals("class")) {
                    Method readMethod = descriptor.getReadMethod();
                    Object  result = readMethod.invoke(bean, new Object[0]);
                    returnMap.put(propertyName, result);
                }
            }
        } catch (IntrospectionException e) {
            System.out.println("分析类属性失败");
        } catch (IllegalAccessException e) {
            System.out.println("实例化 JavaBean 失败");
        } catch (IllegalArgumentException e) {
            System.out.println("映射错误");
        } catch (InvocationTargetException e) {
            System.out.println("调用属性的 setter 方法失败");
        }
        return returnMap;
    }

    public static StandardDatagram getStandardDatagram(PaginationModel page, Map<String, Object> objectMap) {
        StandardDatagram sd;
        PaginationModel paginationModel = page;
        sd = new StandardDatagram.Builder().createSuccess()
                .setData(paginationModel).build();
        return sd;
    }


    /**
     * @param request
     * @return
     * @descrition : 获取并计算分页数据
     * @date :
     * @updateAuthor:
     */
    protected void pageParam(HttpServletRequest request, Map<String, Object> map) {
        int pageSize = 10;
        int currentPage = 1;

        if (null != request.getParameter(PAGENUM)) {
            currentPage = Integer.parseInt(request.getParameter(PAGENUM));
        }
        if (null != request.getParameter(PAGESIZE)) {
            pageSize = Integer.parseInt(request.getParameter(PAGESIZE));
        }

        int offSize = 0;
        //mysql返回参数
        if (currentPage > 0) {
            offSize = (currentPage - 1) * pageSize;
        } else {
            currentPage = 1;
        }
        map.put("pageSize", pageSize);
        map.put("offSize", offSize);
        map.put("currentPage", currentPage);
    }


    /**
     * @param
     * @return
     * @descrition : 获取并计算分页数据
     * @date :
     * @updateAuthor:
     */
    protected void pageParam( Map<String, Object> map) {
        int pageSize = 10;
        int currentPage = 1;

        if (null != map.get(PAGENUM)) {
            currentPage = Integer.parseInt(map.get(PAGENUM).toString());
        }
        if (null != map.get(PAGESIZE)) {
            pageSize = Integer.parseInt(map.get(PAGESIZE).toString());
        }

        int offSize = 0;
        //mysql返回参数
        if (currentPage > 0) {
            offSize = (currentPage - 1) * pageSize;
        } else {
            currentPage = 1;
        }
        map.put("pageSize", pageSize);
        map.put("offSize", offSize);
        map.put("currentPage", currentPage);
    }


    /**
     * @param
     * @return
     * @descrition : 页面分页封装 ， 页面写の
     * @date :
     * @updateAuthor: Carey
     */
    public void page2View(Map modelMap, PaginationModel paginationModel, HttpServletRequest request) {
        String reqUrl = request.getServletPath();
        paginationModel.setUrl(reqUrl);
        String param = request.getParameter("param");
        if (null == param) {
            paginationModel.setParam("{}");
        }
        if (null != modelMap && !modelMap.isEmpty()) {
            paginationModel.setParam(JSONObject.toJSONString(modelMap));
        }
        modelMap.put("paginationModel", paginationModel);
    }


}
